//
//  OptionalChainingViewController.swift
//  SwiftTest
//
//  Created by midea on 16/3/17.
//  Copyright © 2016年 Vincent. All rights reserved.
//

import Foundation

class OptionalChainingViewController : BaseViewController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        replaceRequired()
        getUnderlyingProperties()
    }
    
    fileprivate func replaceRequired(){
        
        class Person {
            var residence: Residence?
        }
        
        class Residence {
            var numberOfRooms = 1
        }
        
        let john = Person()
        
        if let roomCount = john.residence?.numberOfRooms {
            print("John's residence has \(roomCount) room(s).")
        } else {
            print("Unable to retrieve the number of rooms.")
        }
        
        func errorFunc(){
            //====== error reference ========
            let roomCount = john.residence!.numberOfRooms
            print("roomCount is " + String(roomCount))
            //====== error reference ========
        }
    }
    
    fileprivate func getUnderlyingProperties(){
        
        class Person {
            var residence: Residence?
        }
        
        class Residence {
            var rooms : Array<Room> = Array()
            var numberOfRooms : Int {
                return rooms.count
            }
            subscript(i: Int) -> Room {
                return rooms[i] 
            }
            func printNumberOfRooms() {
                print("The number of rooms is \(numberOfRooms)")
            }
            var address: Address?
        }
        
        class Room {
            let name: String
            init(name: String) { self.name = name }
        }
        
        class Address {
            var buildingName: String?
            var buildingNumber: String?
            var street: String?
            func buildingIdentifier() -> String? {
                if buildingName != nil {
                    return buildingName
                } else if buildingNumber != nil {
                    return buildingNumber
                } else {
                    return nil
                }
            }
        }
        
        let person = Person()
        if let count = person.residence?.rooms.count {
            print("count is " + String(count))
        }else {
            print("rooms is nil")
        }
        
        person.residence?.address?.buildingName = "what the hell is it?"
        if let buildingName = person.residence?.address?.buildingName {
            print("buildingName is " + buildingName)
        }else {
            print("buildingName is nil")
        }
        
        if (person.residence?.printNumberOfRooms()) != nil {
            print("It was possible to print the number of rooms.")
        } else {
            print("It was not possible to print the number of rooms.")
        }
        
        if let firstRoomName = person.residence?[0].name {
            print("The first room name is \(firstRoomName).")
        } else {
            print("Unable to retrieve the first room name.")
        }
        
        let johnsHouse = Residence()
        johnsHouse.rooms.append(Room(name: "Living Room"))
        johnsHouse.rooms.append(Room(name: "Kitchen"))
        person.residence = johnsHouse
        
        if let firstRoomName = person.residence?[0].name {
            print("The first room name is \(firstRoomName).")
        } else {
            print("Unable to retrieve the first room name.")
        }
    }
}
